﻿
var tag_cerent = 1;
var pagecount = "0", pagecount3;
var page = 1;

$(function () {
    bindClick();
    init();
    $(window).bind("scroll", function () {
        //当滚动条滚动时
        var scollTop = $(this).scrollTop() + $(this).height();
        var scollH = $(document).height();

        if (scollH - scollTop < 100) {
            if (wait.isShow) { return; } //如果正在加载，则返回  
            page++;
            if (tag_cerent == 1)
                GetList_FL(page);
            else
                GetList(page);
        }
        return false;
    });
});

function init() {
    var tag = getQueryString("tag");
    if (tag == 2)
        $(".tab").eq(1).click();
    else if (tag == 3)
        $(".tab").eq(2).click();
    else
        $(".tab").eq(0).click();

    $(".back").bind("click", function () {
        if (document.referrer == null || document.referrer.toString().indexOf("fl_detail") > -1)
            document.location.href = 'personal.html'
        else
            goBack();
    });
}

//绑定点击事件
function bindClick() {
    $(".tab").on("click", function () {
        $(".tab").removeClass("tab-active");
        $(this).addClass("tab-active");

        //得到选项卡序号
        var tabIndex = $(this).attr("tab");
        $("div[tabindex]").hide();
        $("div[tabindex = '" + tabIndex + "']").show();
        tag_cerent = tabIndex;
        $(".tg_select" + tag_cerent).html("");
        page = 1;
        if (tag_cerent == 1)
            GetList_FL(1);
        else
            GetList(1);
    });
}


//返利列表
function GetList(page) {
    if ($("#pagecount" + tag_cerent).val() == "1") { return; }
    if (!islogin()) { window.location.href = 'login.html'; return; }
    var param = "page=" + page + "&modelid=" + tag_cerent;
    AjaxStart("ajax/fc_record.ashx?getrecordlist", param, function (data) {
        if (data.code == "4") { Alert(data.msg.toString()); $("#pagecount" + tag_cerent).val("1"); return; }
        
        if (data.item.pageCount > 0 && data.item.pageCount < page) {
            if ($("#pagecount" + tag_cerent).val() == "0")
                Alert('到底了！');
            $("#pagecount" + tag_cerent).val("1"); return;
        }
        loadhtml(data);
    });
}

function loadhtml(data) {
    $(data.item.table).each(function () {
        var discount = (this.discount == null ? 0 : this.discount) * 0.01;
        var c = "bg-hui";
        if (this.op_remainder == 0 && this.rebate_state == 1)
            c = "bg-yellow";
        var title = this.title;
        if (title.length > 32)
            title = title.substring(0, 32);
        $(".tg_select" + tag_cerent).append("<div class='pl10 pr10 pt5 pb5 bb' onclick=\"toUrl('fl_detail.html?id=" + this.id + "&tag=" + tag_cerent + "')\"><div class='media'><div class='media-body'>" +
            "<div class='fc-title'>" + title + "</div>" +
            "<div class='fc-num'><img src='img/hb.png' /><span class='gray'>" + this.op_total + "</span>/<span class='red'>" + (this.op_total - this.op_remainder) + "</span></div></div>" +
            "<div class='media-right'><div class='b text-center gray'><div class='pl10 pr10'><div>金额</div>" +
            "<div>￥" + (parseFloat(parseFloat(this.op_total * this.op_price).toFixed(2))) + "</div>" +
            "</div><div class='pl10 pr10 back-m white " + c + "'>" +
            "<div>返利</div><div>￥" + (parseFloat(parseFloat(this.op_total * this.op_price * discount).toFixed(2))) +
            "</div></div></div></div></div></div>");
    });
}

//客户列表
function GetList_FL(page2) {
    if (pagecount == "1") { return; }
    if (!islogin()) { window.location.href = 'login.html'; return; }
    AjaxStart("ajax/fc_record.ashx?getclientlist", "&page_index=" + page, function (data) {
        if (data.code == "4") { Alert(data.msg.toString()); pagecount = "1"; return; }
        if (data.item.pageCount < page) {
            if (pagecount == "0")
                Alert('到底了！');
            pagecount = "1"; return;
        }
        $(".countnum").html(data.item.itemCount + " 条");
        loadhtml_FL(data);
    });
}

function loadhtml_FL(data) {
    $(data.item.table).each(function () {
        $(".tg_select" + tag_cerent).append("<div class='pt10 pb10 bb'><div class='media'>" +
            "<div class='media-left'><div class='left-date'>" + this.date.replace("T", " ").substring(0, 19) + "</div></div>" +
            "<div class='media-body'><div class='client-body'>" +
            "<div>" + this.nick_name + "</div>" +
            "<div>" + this.id + " / " + this.phone + "</div></div></div></div></div>");
    });
}